package cn.dankal.www.uitest;

import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerview;
    private LinearLayoutManager linearLayoutManager;
    RecyclerViewAdapter recyclerViewAdapter;
    private List<HeadBean> headBean = new ArrayList<>();
    private List<BodyBean> bodyBean = new ArrayList<>();
    private List<FootBean> footBean = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setItemAnimator(new DefaultItemAnimator());
//        recyclerViewAdapter = new RecyclerViewAdapter(this, headBean);
//        recyclerViewAdapter = new RecyclerViewAdapter(this, headBean,bodyBean);
        recyclerViewAdapter = new RecyclerViewAdapter(this, headBean, bodyBean, footBean);
        recyclerview.setAdapter(recyclerViewAdapter);
        recyclerViewAdapter.setOnBodyItemOnClickListener(new RecyclerViewAdapter.OnItemOnClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this, bodyBean.get(position).getBodyItemName(), Toast.LENGTH_SHORT).show();
            }
        });
        initData();
    }

    private void initData() {
        initHeadData();
        initBodyData();
        initFootData();
        recyclerViewAdapter.initDate();
    }

    private void initHeadData() {
        HeadBean mHeadBean = null;
        for (int i = 0; i < 1; i++) {
            mHeadBean = new HeadBean();
            mHeadBean.setTitle(getResources().getString(R.string.welcome));
            mHeadBean.setContent1(getResources().getString(R.string.from));
            mHeadBean.setContent2(getResources().getString(R.string.author));
            mHeadBean.setContent3(getResources().getString(R.string.opinion));
            headBean.add(mHeadBean);
        }
    }

    private void initBodyData() {
        String[] bodyName = getResources().getStringArray(R.array.bodyName);

        TypedArray ar = getResources().obtainTypedArray(R.array.bodyIcon);
        int len = ar.length();
        int[] bodyIcon = new int[len];
        for (int i = 0; i < len; i++)
            bodyIcon[i] = ar.getResourceId(i, 0);
        ar.recycle();

        BodyBean mBodyBean = null;
        for (int i = 0; i < bodyName.length; i++) {
            mBodyBean = new BodyBean();
            mBodyBean.setBodyItemName(bodyName[i]);
            mBodyBean.setBodyItemIcon(bodyIcon[i]);
            bodyBean.add(mBodyBean);
        }
    }

    private void initFootData() {
        String[] footName = getResources().getStringArray(R.array.footName);

        TypedArray ar = getResources().obtainTypedArray(R.array.footIcon);
        int len = ar.length();
        int[] footIcon = new int[len];
        for (int i = 0; i < len; i++)
            footIcon[i] = ar.getResourceId(i, 0);
        ar.recycle();

        FootBean mFootBean = null;
        for (int i = 0; i < footName.length; i++) {
            mFootBean = new FootBean();
            mFootBean.setFootItemName(footName[i]);
            mFootBean.setFootItemIcon(footIcon[i]);
            footBean.add(mFootBean);
        }
    }
}
